﻿using AutoMapper;
using Pay.DTO;
using Pay.Data;
using Pay.IService;
using Pay.Core;
using Pay.TO;
using System;

namespace Pay.Service.AutoMapper {
    public class RedRecordMapper : IStartupDto {
        private IMapperConfigurationExpression _cfg;
        public RedRecordMapper(IMapperConfigurationExpression cfg) {
            _cfg = cfg;
        }
        public void Execute() {
            Create();
        }
        protected virtual void Create() {
            _cfg.CreateMap<RedRecord, token_user_dto>()
                .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
                .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.MerchantId))
                .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.CreateTime.ToDateTime()))
                .ForMember(dest => dest.total_horse_power, src => src.MapFrom(c => c.TotalHorsePower))
                .ForMember(dest => dest.total_red_drill, src => src.MapFrom(c => c.TotalRedDrill))
                .ForMember(dest => dest.consume_red_drill, src => src.MapFrom(c => c.ConsumeRedDrill))
                .ForMember(dest => dest.invitation_code, src => src.MapFrom(c => c.InvitationCode))
                .ForMember(dest => dest.invitation_num, src => src.MapFrom(c => c.InvitationCodeNum))
                .ForMember(dest => dest.name, src => src.MapFrom(c => c.Merchant.Name))
                .ForMember(dest => dest.mobile, src => src.MapFrom(c => c.Merchant.Mobile))
                .ForMember(dest => dest.disabled, src => src.MapFrom(c => c.Merchant.Disabled))
                .ForMember(dest => dest.user_type, src => src.MapFrom(c => c.Merchant.UserType))
                .ForMember(dest => dest.user_type_value, src => src.MapFrom(c => c.Merchant.UserType != null ? ((int)c.Merchant.UserType).ToString() : ((int)UserType.user).ToString()));


            _cfg.CreateMap<RedRecord, red_user_dto>()
                .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
                .ForMember(dest => dest.nick_name, src => src.Ignore())
                .ForMember(dest => dest.red_drill, src => src.MapFrom(c => c.TotalRedDrill))
                .ForMember(dest => dest.horse_power, src => src.MapFrom(c => c.TotalHorsePower))
                .ForMember(dest => dest.frozen_red_drill, src => src.MapFrom(c => c.FrozenRedDrill))
                .ForMember(dest => dest.present_red_drill, src => src.MapFrom(c => c.TotalRedDrill - c.ConsumeRedDrill))
                .ForMember(dest => dest.invitation_code, src => src.MapFrom(c => c.InvitationCode))
                .ForMember(dest => dest.invitation_code_num, src => src.MapFrom(c => c.InvitationCodeNum > 10 ? 0 : 10 - c.InvitationCodeNum))
                .ForMember(dest => dest.invitation_num, src => src.MapFrom(c => c.InvitationCodeNum));
            _cfg.CreateMap<RedRecordDetail, redrecorddetail_dto>()
                .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
                .ForMember(dest => dest.red_drill, src => src.MapFrom(c => c.RedDrill));
            _cfg.CreateMap<RedProductRecordDetail, redrecorddetail_dto>()
               .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
               .ForMember(dest => dest.red_drill, src => src.MapFrom(c => c.RedDrill));
            _cfg.CreateMap<RedRecordDetail, user_record_dto>()
                .ForMember(dest => dest.time, src => src.MapFrom(c => c.CreateTime.ToDateTime()))
                .ForMember(dest => dest.title, src => src.MapFrom(c => c.Type == 1 ? "消费" : "日常领取"))
                .ForMember(dest => dest.horse_power, src => src.Ignore())
                .ForMember(dest => dest.red_drill, src => src.MapFrom(c => c.Type == 1 ? -c.RedDrill : c.RedDrill))
                .AfterMap((src, dest) => {
                    string title = string.Empty;
                    switch (src.Type) {
                        case 1: title = "消费"; dest.red_drill = -src.RedDrill; break;
                        case 2: title = "日常领取"; dest.red_drill = src.RedDrill; break;
                        case 3: title = "活动消费"; dest.red_drill = -src.RedDrill; break;
                        case 4: title = "活动获取"; dest.red_drill = src.RedDrill; break;
                        case 5: title = "购买商品"; dest.red_drill = -src.RedDrill; break;
                        case 6: title = "卖出商品"; dest.red_drill = src.RedDrill; break;
                        default: title = "消费"; dest.red_drill = -src.RedDrill; break;
                    }
                    dest.title = title;
                });
            _cfg.CreateMap<UserSoupValueHis, user_record_dto>()
                .ForMember(dest => dest.time, src => src.MapFrom(c => c.CreateTime.ToDateTime()))
                .ForMember(dest => dest.title, src => src.MapFrom(c => c.TaskType.ToDescription()))
                .ForMember(dest => dest.horse_power, src => src.MapFrom(c => c.TaskType == TO.ETaskType.NoLogin ? -c.SoupValue : c.SoupValue))
                .ForMember(dest => dest.e_time, src => src.MapFrom(c => c.EffectiveTime.ToDateTime()))
                .ForMember(dest => dest.red_drill, src => src.Ignore());
        }
    }
}
